package com.yale.spring.cloud.code.controller;

import com.yale.spring.cloud.commons.OpenResponse;
import com.yale.spring.cloud.commons.OpenResponseCode;
import com.yale.spring.cloud.commons.model.MailInfo;
import com.yale.spring.cloud.feigin.api.CodeService;
import com.yale.spring.cloud.feigin.api.MailService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author yale
 */
@Controller
@RequestMapping("/api/code")
public class CodeController {

    @DubboReference
    private MailService mailService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CodeService codeService;


    @GetMapping("/sendCode/{mail}")
    @ResponseBody
    public OpenResponse sendCode(@PathVariable String mail){
        codeService.generateCode(mail);
        return new OpenResponse();
    }

    @GetMapping("/verifyCode/{mail}/{code}")
    @ResponseBody
    public OpenResponse verifyCode(@PathVariable String mail,@PathVariable String code){
        String codeSend=(String)redisTemplate.opsForValue().get(mail+"_code");
        if(StringUtils.isEmpty(codeSend)|| !code.equals(codeSend)){
            return new OpenResponse(OpenResponseCode.VERIFY_FAILED);
        }
        return new OpenResponse();
    }

 
}
